<template>
  <div>

    <el-dialog
      v-if="dialogVisible"
      title="请指定项目负责人"
      :visible.sync="dialogVisible"
      width="400px"
      :before-close="handleClose">

      <el-form :model="datas" class="demo-form-inline" label-width="120px" label-position="left">
        <el-form-item label="临床阶段负责人">
<!--          <el-cascader v-model="datas.value"-->
<!--                       :show-all-levels="false"-->
<!--                       :options="departments"-->
<!--                       :props="props"-->
<!--                       :placeholder="datas.project_person_in_charge"-->
<!--                       :change-on-select="false"-->
<!--                       @change="cascaderChange">-->
<!--            <template slot-scope="{ node, data }">-->
<!--              <span v-if="node.level == 1">{{ data.department_name }}</span>-->
<!--              <span v-else>{{ data.user_name }}</span>-->
<!--            </template>-->
<!--          </el-cascader>-->

          <el-cascader
            :show-all-levels="false"
            v-model="datas.value"
            placeholder="请选择负责人"
            :options="options11"
            @change="handleChange"
            filterable></el-cascader>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmClick">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
    export default {
        name: "projectPerson",
        props:{
            datas:{
                type:Object,
                default:function () {
                    return{}
                }
            }
        },
        data(){
            return{
                dialogVisible:false,
                departments:[],
                props: {
                    value:'id',
                    label:'department_name',
                    lazy: true,
                    lazyLoad : ((node, resolve) => {

                        if (node.level == 1){
                            if (node.data){
                                let d = {
                                    department_id:node.data.id,
                                    limit:1000,
                                    skip:1,
                                }

                                this.$api.GET_USER_LIST(d).then(res=>{
                                    if (res.code == 200){

                                        for (let i = 0; i < res.data.data.length; i++){
                                            res.data.data[i].leaf = true;
                                            res.data.data[i].department_name = res.data.data[i].user_name
                                        }
                                        this.departmentMembers = res.data.data;
                                        resolve(res.data.data);
                                    }else{
                                        this.$message.error(res.msg)
                                    }
                                })
                            }
                        }
                    })
                },
                options11:[]

            }
        },
        created:function(){
            this.$api.USER_GROUP_DEPARTMENT_LIST().then(res=>{
                if (res.code == 200){
                    res.data.data.forEach(mem=>{
                        mem.label = mem.name
                        mem.value = mem.id
                        if (!mem.children || !mem.children.length){
                            mem.leaf = true
                        }else if (mem.children.length) {
                            mem.children.forEach(item=>{
                                item.leaf = true;
                                item.value = item.id;
                                item.label = item.name;
                                item.department_id = item.id;
                            })
                        }
                    })
                    this.options11 = res.data.data;
                }else{
                    this.$message.error(res.msg)
                }
            });
        },
        mounted() {
            this.departments = this.$store.getters.department
        },
        methods:{
            handleChange:function(value){
                if (value && value[1]){
                    let index = this.options11.findIndex(a=>a.id == value[0])
                    if (index != -1){
                        let dep = this.options11[index]
                        let index1 = dep.children.findIndex(a=>a.id == value[1])
                        if (index1 != -1){
                            this.datas.project_person_in_charge_id = dep.children[index1].id
                            this.datas.project_person_in_charge = dep.children[index1].name
                        }
                    }
                }
            },
            openDialog:function(){
                this.departments.forEach((item)=>{
                    item.disable = false
                })
                this.dialogVisible = true;
            },
            handleClose:function(){
                this.dialogVisible = false;
            },
            cascaderChange:function(node){
                if(node && node.length){
                    let a = this.departmentMembers.findIndex(a=>a.id == node[node.length-1])
                    if (a != -1){
                        //this.value = this.value + "/" + this.departmentMembers[a].user_name
                        this.datas.project_person_in_charge_id = this.departmentMembers[a].id
                        this.datas.project_person_in_charge = this.departmentMembers[a].user_name
                    }
                }

            },
            confirmClick:function () {

                if (!this.datas.project_person_in_charge_id){
                    this.$message.error("请选择项目负责人")
                    return
                }
                this.dialogVisible = false;

                this.$emit('success')
                // this.$api.PROJECT_MANAGER_PROJECT_CREATE_STAGE(this.datas).then(res=>{
                //     if (res.code == 200){
                //         this.$router.push({name:"projectManagementEdit",query:{id:res.data}})
                //     }else {
                //         this.$message.error(res.msg)
                //     }
                // })
            },
        }
    }
</script>

<style scoped>

</style>
